#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 10001;

int n, m, dfn[N], low[N], sz[N], val[N], belong[N], in[N], dp[N], idx, sc;
bool vis[N];
stack<int> st;
vector<int> edge[N], edge2[N];

void tarjan(int u, int fa)
{
    dfn[u] = low[u] = ++idx, st.push(u), vis[u] = true;
    for (int x : edge[u]) {
        if (!dfn[x]) {
            tarjan(x, u);
            low[u] = min(low[u], low[x]);
        } else if (vis[x])
            low[u] = min(low[u], dfn[x]);
    }
    if (dfn[u] == low[u]) {
        ++sc;
        while (st.top() != u) {
            sz[sc] += val[st.top()];
            vis[st.top()] = false;
            belong[st.top()] = sc;
            st.pop();
        }
        sz[sc] += val[u], vis[u] = false, belong[u] = sc;
        st.pop();
    }
}

void topo()
{
    queue<int> q;
    for (int i = 1; i <= sc; i++)
        if (!in[i])
            q.push(i), dp[i] = sz[i];
    while (!q.empty()) {
        int s = q.front();
        q.pop();
        for (int x : edge2[s]) {
            dp[x] = max(dp[x], dp[s] + sz[x]);
            if (--in[x] == 0)
                q.push(x);
        }
    }
}

signed main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> val[i];
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        edge[x].push_back(y);
    }
    for (int i = 1; i <= n; i++)
        if (!dfn[i])
            idx = 0, tarjan(i, i);
    for (int i = 1; i <= n; i++)
        for (int x : edge[i])
            if (belong[i] != belong[x])
                edge2[belong[i]].push_back(belong[x]), in[belong[x]]++;
    topo();
    int ans = 0;
    for (int i = 1; i <= sc; i++)
        ans = max(ans, dp[i]);
    cout << ans << endl;

    return 0;
}
